import api from "../common/api";

const userInfo = {
  vip: '', // vip状态 0:正常 1:禁用 2:到期 3:非会员
  officialAccount: '', // 公众号状态 0:取消关注 1:已关注 2:从未关注
  subscribeSaleType: '', // 业务范围 0:租售 1:二手 2:出租
  subscribeInfoType: '', // 房源情报类型 0:新上 1:带看 2:调价 3:下架
}

const updateUserInfo = (info) => {
  if (!info) return;
  Object.assign(userInfo, info)
}

const safetyUpdate = (info) => {
  if (!info) return;
  let keys = Object.keys(info);
  keys.forEach((key, index) => {
    if (info[key]) {
      userInfo[key] = info[key];
    }
  });
}
// -------------------------------vip-------------------------------------
// reacquire 重新请求接口
const getVip = (reacquire = false) => {
  return new Promise((resolve, reject) => {
    if (userInfo.vip && !reacquire) {
      resolve(userInfo.vip)
      return
    }
    const memberAgentId = getApp().globalData.loginUserInfo.data.content.sessionUser.id;
    if (!memberAgentId) {
      return reject()
    }
    api.getMemberList({
      "currentPage": 1,
      "pageSize": 1,
      "t": {
        "memberAgentId": memberAgentId
      }
    }, (res) => {
      if (res && res.data && res.data.isSuccess) {
        updateUserInfo({
          vip: res.data.content.statue
        })
        resolve(res.data.content.statue)
      } else {
        reject()
      }
    })
  })
}

const updateVip = () => {
  return new Promise((resolve, reject) => {
    const memberAgentId = getApp().globalData.loginUserInfo.data.content.sessionUser.id;
    if (!memberAgentId) {
      return reject()
    }
    api.getMemberList({
      "currentPage": 1,
      "pageSize": 1,
      "t": {
        "memberAgentId": memberAgentId
      }
    }, (res) => {
      if (res && res.data && res.data.isSuccess) {
        updateUserInfo({
          vip: res.data.content.statue
        })
        resolve(res.data.content.statue)
      } else {
        reject()
      }
    })
  })
}


// ----------------------------业务范围-------------------------------------
const getSubscribeRange = () => {
  return new Promise((resolve, reject) => {
    if (userInfo.subscribeSaleType) {
      resolve({
        subscribeSaleType: userInfo.subscribeSaleType,
        subscribeInfoType: userInfo.subscribeInfoType,
      })
      return
    }
    api.getServiceScope({}, (res) => {
      if (res && res.data && res.data.content) {
        const {
          scopeState = '-1',
            intelligenceType = '-1'
        } = res.data.content
        updateUserInfo({
          subscribeSaleType: scopeState.toString(),
          subscribeInfoType: intelligenceType.toString(),
        })
        resolve({
          subscribeSaleType: userInfo.subscribeSaleType,
          subscribeInfoType: userInfo.subscribeInfoType,
        })
      } else {
        reject()
      }
    })
  })
}

const updateSubscribeRange = () => {
  return new Promise((resolve, reject) => {
    api.getServiceScope({}, (res) => {
      if (res && res.data && res.data.content) {
        const {
          scopeState = '-1',
            intelligenceType = '-1'
        } = res.data.content
        updateUserInfo({
          subscribeSaleType: scopeState.toString(),
          subscribeInfoType: intelligenceType.toString(),
        })
        resolve({
          subscribeSaleType: userInfo.subscribeSaleType,
          subscribeInfoType: userInfo.subscribeInfoType,
        })
      } else {
        reject({})
      }
    })
  })
}

const saveSubscribeRange = ({
  subscribeSaleType,
  subscribeInfoType
}) => {
  return new Promise((resolve, reject) => {
    api.saveServiceScope({
      scopeState: Number(subscribeSaleType),
      intelligenceType: subscribeInfoType,
    }, (res) => {
      if (res && res.data && res.data.isSuccess) {
        updateUserInfo({
          subscribeSaleType,
          subscribeInfoType,
        })
        resolve()
      } else {
        reject()
      }
    })
  })
}
// ----------------------------公众号-------------------------------------
const getOfficialAccount = (reacquire = false) => {
  return new Promise((resolve, reject) => {
    if (userInfo.officialAccount && !reacquire) {
      resolve(userInfo.officialAccount)
      return
    }
    api.checkIsFollowOfficial({}, (res) => {
      if (res && res.data && res.data.isSuccess && res.data.content) {
        updateUserInfo({
          officialAccount: res.data.content.fockStated.toString() || '-1'
        })
        resolve(userInfo.officialAccount)
      } else {
        reject()
      }
    })
  })
}

const updateOfficialAccount = () => {
  return new Promise((resolve, reject) => {
    api.checkIsFollowOfficial({}, (res) => {
      if (res && res.data && res.data.isSuccess && res.data.content) {
        updateUserInfo({
          officialAccount: res.data.content.fockStated.toString() || '-1'
        })
        resolve(userInfo.officialAccount)
      } else {
        reject()
      }
    })
  })
}


// ====================业主房源的业务范围===========================

// 保存业务范围
const save_service_scope = ({
  subscribeSaleType,
  subscribeInfoType
}) => {
  return new Promise((resolve, reject) => {
    api.save_service_scope({
      scopeState: Number(subscribeSaleType)
    }, (res) => {
      if (res && res.data && res.data.isSuccess) {
        updateUserInfo({
          subscribeSaleType,
          subscribeInfoType,
        })
        resolve()
      } else {
        reject()
      }
    })
  })
}
// 查询业务范围
const get_service_scope = () => {
  return new Promise((resolve, reject) => {
    if (userInfo.subscribeSaleType) {
      resolve({
        subscribeSaleType: userInfo.subscribeSaleType,
      })
      return
    }
    api.get_service_scope({}, (res) => {
      if (res && res.data && res.data.content) {
        const {
          scopeState = '-1'
        } = res.data.content
        updateUserInfo({
          subscribeSaleType: scopeState.toString(),
        })
        resolve({
          subscribeSaleType: userInfo.subscribeSaleType,
        })
      } else {
        reject()
      }
    })
  })
}
// -----------------------------------------------------------------
export {
  getSubscribeRange,
  updateSubscribeRange,
  getOfficialAccount,
  updateOfficialAccount,
  saveSubscribeRange,
  getVip,
  updateVip,
  save_service_scope,
  get_service_scope
}